import { app, Application, Session } from "mydog";
import { DEF } from "../../../app/definition/consts";
import { connector_msg } from "../../../config/proto/connector";

export default class Handler {
  app: Application;
  uid: number = 1;
  constructor(app: Application) {
    this.app = app;
  }

  /**
   * login
   * @param msg message from client
   * @param session
   * @returns
   */
  public async login(msg: connector_msg.c2s_login, session: Session, next: Function) {
    const username = msg.username;
    const password = msg.password;

    const res: connector_msg.s2c_login = new connector_msg.s2c_login();

    if (!!!username || !!!password) {
      res.code = DEF.SYSTEM.LOGIN_CODE.FAIL_WRONG;
      next(res);
      return res;
    }

    const user = await this.app.rpcAwait(DEF.SEVER.ID.DATABASE).database.main.getUserByUsername(username);
    if (!!user) {
      // Duplicate login
      const userInfo = await this.app.rpcAwait(DEF.SEVER.ID.LOBBY).lobby.main.getUserByUsername(username);
      if (!!userInfo && !!userInfo.isOnline) {
        res.code = DEF.SYSTEM.LOGIN_CODE.FAIL_DUPLICATE;
        next(res);
        return res;
      }

      // Login result
      if (user.password === password) {
        // bind uid
        session.bind(user.id);

        const token = await this.app.rpcAwait(DEF.SEVER.ID.GATE).gate.main.tokenSign(user.id);
        // push user into lobby
        const userInfo = { id: user.id, username: user.username, nickname: user.nickname, sid: session.sid, token: token }
        this.app.rpc(DEF.SEVER.ID.LOBBY).lobby.main.addUser(userInfo);

        // return
        res.code = DEF.SYSTEM.LOGIN_CODE.SUCCESS;
        res.user = { id: user.id, username: user.username, nickname: user.nickname };
        res.token = token;
        next(res);
        return res;
      } else {
        res.code = DEF.SYSTEM.LOGIN_CODE.FAIL_WRONG;
        next(res);
        return res;
      }
    }
    res.code = DEF.SYSTEM.LOGIN_CODE.FAIL_NOT_EXIST;
    next(res);
    return res;
  }

  /**
   * logout
   * @param msg
   * @param session
   */
  public logout(msg: connector_msg.c2s_logout, session: Session) {
    const uid = msg.uid;

    const res: connector_msg.s2c_logout = new connector_msg.s2c_logout();

  }

  /**
   * register
   * @param msg message from client
   * @param session
   * @returns
   */
  public async register(msg: connector_msg.c2s_register, session: Session, next: Function) {
    const username = msg.username;
    const password = msg.password;

    const res: connector_msg.s2c_register = new connector_msg.s2c_register();

    if (!!!username || !!!password) {
      res.code = DEF.SYSTEM.RETURN_CODE.ERROR;
      next(res);
      return res;
    }

    const user = await this.app.rpcAwait(DEF.SEVER.ID.DATABASE).database.main.registerUser(username, password);
    if (!!user) {
      res.code = DEF.SYSTEM.RETURN_CODE.SUCCESS;
      res.user = { id: user.id, username: user.username, nickname: user.nickname };
      next(res);
      return res;
    }

    res.code = DEF.SYSTEM.RETURN_CODE.ERROR;
    next(res);
    return res;
  }

  /**
   * client disconnect
   * @param session current session object
   */
  private onClientDisconnect(session: Session) {
    if (!session || !session.uid) {
      return;
    }
  }
}

/**
 * client disconnect
 * @param session
 */
export function onClientDisconnect(session: Session) {
  if (!session || !session.uid) {
    return;
  }

  console.log(`----------------------- onClientDisconnect uid:${session.uid} -----------------------`)
  // remove user out lobby
  app.rpc(DEF.SEVER.ID.LOBBY).lobby.main.removeUser(session.uid, session.sid);

  // const rid = session.get('rid');
  // console.log('---------------- onUserDisconnect rid: ' + rid + ' -----------------------')
  // // room
  // if (!!rid) {
  //   this.app.rpc.room.roomRemote.onUserDisconnect.route(session, true)(session.uid, sid);
  // }
}
